from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# 初始化浏览器驱动
driver = webdriver.Chrome()

# 访问百度首页
url = "https://www.baidu.com"
driver.get(url)
driver.maximize_window()

try:
    # 等待“新闻”链接出现并点击
    news_link = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.LINK_TEXT, "新闻"))
    )
    news_link.click()

    # 等待新页面加载并截图
    time.sleep(3)
    driver.save_screenshot("baidu_news_page.png")
    print("截图已保存")

except Exception as e:
    print(f"操作失败：{e}")

finally:
    # 关闭浏览器
    driver.quit()